package avicit.bdp.dgs.mdm.rest;

import avicit.bdp.dgs.mdm.dto.CodeDTO;
import avicit.bdp.dgs.mdm.service.CodeService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-12-06
 * @类说明：CodeRest
 * @修改记录：
 * @注意事项：
 * @主要功能：编码中字段编码Rest接口
 */
@RestController
@Api(tags = "mdm", description = "编码中字段编码Rest接口")
@RequestMapping("/api/bdp/dgs/mdm/code")
public class CodeRest {
    @Autowired
    private CodeService codeService;

    /**
     * 新增编码
     *
     * @param codeDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增编码")
    public ResponseMsg<String> addCode(@RequestBody CodeDTO codeDTO) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.codeService.addCode(codeDTO));

        return responseMsg;
    }

    /**
     * 删除/批量删除编码，多个id使用英文逗号隔开
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除/批量删除编码 ，多个id使用英文逗号隔开")
    public ResponseMsg<Integer> deleteCode(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.codeService.deleteCode(id));

        return responseMsg;
    }

    /**
     * 修改编码
     *
     * @param codeDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改编码")
    public ResponseMsg<Boolean> updateCode(@RequestBody CodeDTO codeDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.codeService.updateCode(codeDTO));

        return responseMsg;
    }

    /**
     * 查询编码详情
     *
     * @param id 编码ID
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "查询编码详情")
    public ResponseMsg<CodeDTO> getCodeDetail(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<CodeDTO> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.codeService.getCodeDetail(id));

        return responseMsg;
    }

    /**
     * 分页查询编码
     *
     * @param modelId
     * @param keyWords
     * @param code
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/query")
    @ApiOperation(value = "分页查询编码")
    public ResponseMsg<QueryRespBean<CodeDTO>> queryCode(
            @ApiParam(value = "模型Id", name = "modelId") @RequestParam String modelId,
            @ApiParam(value = "查询关键字", name = "keyWords") @RequestParam(required = false) String keyWords,
            @ApiParam(value = "编码", name = "code") @RequestParam(required = false) String code,
            @ApiParam(value = "当前页", name = "pageNo") @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @ApiParam(value = "每页大小", name = "pageSize") @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        ResponseMsg<QueryRespBean<CodeDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.codeService.queryCode(modelId, keyWords, code, pageNo, pageSize));

        return responseMsg;
    }

    /**
     * 编码是否重复
     * 注意：一个字段仅有一个编码规则
     *
     * @param id       编码ID，编辑时修改名称，需要传入idd;新增场景不需要传入
     * @param columnId 字段ID
     * @param ruleId   主题ID
     * @return
     */
    @GetMapping("/isDuplicateCode")
    @ApiOperation(value = "编码是否重复")
    public ResponseMsg<Boolean> isDuplicateCode(@ApiParam(value = "编码ID", name = "id") @RequestParam(required = false) String id,
                                           @ApiParam(value = "字段ID", name = "columnId") @RequestParam(required = false) String columnId,
                                           @ApiParam(value = "主题ID", name = "ruleId") @RequestParam(required = false) String ruleId) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.codeService.isDuplicateCode(id, columnId, ruleId));

        return responseMsg;
    }
}
